<template>
  <div class="app-container">

    <div class="search-div">
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="订单状态">
              <el-select v-model="searchObj.orderStatus" placeholder="订单状态">
                <el-option label="未接单" :value="1" />
                <el-option label="进行中" :value="2" />
                <el-option label="已完成" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" :loading="loading" @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
    >

      <el-table-column
        label="序号"
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="pname" label="产品名" width="300">
        <template slot-scope="scope">
          {{ scope.row.pname.join(', ') }}
        </template>
      </el-table-column>
      <el-table-column prop="pcount" label="产品数量" width="300" />
      <el-table-column prop="orderTime" label="下单时间" width="332" />
      <el-table-column label="订单状态" width="350">
        <template slot-scope="scope">
          <div :style="getStatusStyle(scope.row.orderStatus)" class="centered-text">
            {{ getStatusText(scope.row.orderStatus) }}
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<style scoped>
.centered-text {
  display: flex;
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
  height: 100%; /* 让内容占据整个单元格的高度 */
}
</style>

<script>
import api from '@/api/system/PersonalOrder'

const defaultForm = {
  id: '',
  pid: '',
  pcount: 0,
  orderStatus: '',
  orderTime: '',
  uid: '',
  uname: '',
  pname: '',
  ostatus: ''

}
const defaultForm1 = {
  id: '',
  username: '',
  password: '',
  name: '',
  phone: '',
  sex: '',
  account: '',
  roleName: '',

}

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      records: 0,
      searchObj: {}, // 查询表单对象
      createTimes: [],

      dialogVisible: false,
      Order: defaultForm,
      saveBtnDisabled: false,
      dialogRoleVisible: false,
      isIndeterminate: false, // 是否是不确定的
      checkAll: false, // 是否全选
      selectedDeviceCount: 0
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },

    // 加载banner列表数据
    fetchData(page = 1) {
      this.page = page
      console.log('翻页。。。' + this.page)

      api.index(this.page, this.limit, this.searchObj).then(
        response => {
          // this.list = response.data.list
          this.list = response.data.records
          this.total = response.data.total

          // 数据加载并绑定成功
          this.listLoading = false
        }
      )
    },

    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },
    fetchDataById(id) {
      api.getById(id).then(response => {
        this.Order = response.data
      })
    },
    getStatusStyle(status) {
      let style = {}
      switch (status) {
        case 3:
          style = {
            background: 'linear-gradient(to right, #56ab2f, #a8e063)' // 绿色渐变背景
            // boxShadow: '0px 4px 14px rgba(86, 171, 47, 0.5)', // 绿色阴影
          }
          break
        case 2:
          style = {
            background: 'linear-gradient(to right, #f09819, #edde5d)' // 黄色渐变背景
            // boxShadow: '0px 4px 14px rgba(240, 152, 25, 0.5)', // 黄色阴影
          }
          break
        case 1:
          style = {
            background: 'linear-gradient(to right, #bdc3c7, #2c3e50)' // 灰色渐变背景
            // boxShadow: '0px 4px 14px rgba(189, 195, 199, 0.5)', // 灰色阴影
          }
          break
        default:
          break
      }
      return {
        ...style,
        color: 'white', // 文字颜色为白色
        padding: '5px 10px', // 添加一些填充效果
        borderRadius: '5px' // 圆角效果
      }
    },
    getStatusText(status) {
      switch (status) {
        case 1:
          return '未接单'
        case 2:
          return '进行中'
        case 3:
          return '已完成'
        default:
          return '未知'
      }
    }
  }
}
</script>
